#include "wx/wxprec.h"
#include "wx/xrc/xh_wizrd.h"
#include "wx/wizard.h"

IMPLEMENT_DYNAMIC_CLASS( wxWizardXmlHandler, wxXmlResourceHandler )

wxWizardXmlHandler::wxWizardXmlHandler() : wxXmlResourceHandler() {
  m_wizard = NULL;
  m_lastSimplePage = NULL;
  XRC_ADD_STYLE( wxWIZARD_EX_HELPBUTTON );
  AddWindowStyles();
}

wxObject *wxWizardXmlHandler::DoCreateResource() {
  if( m_class == wxT( "wxWizard" ) ) {
    XRC_MAKE_INSTANCE( wiz, wxWizard )
    long style = GetStyle( wxT( "exstyle" ), 0 );
    if( style != 0 ) {
      wiz->SetExtraStyle( style );
    }
    wiz->Create( m_parentAsWindow,
                 GetID(),
                 GetText( wxT( "title" ) ),
                 GetBitmap(),
                 GetPosition() );
    SetupWindow( wiz );
    wxWizard *old = m_wizard;
    m_wizard = wiz;
    m_lastSimplePage = NULL;
    CreateChildren( wiz, true );
    m_wizard = old;
    return wiz;
  } else {
    wxWizardPage *page;
    if( m_class == wxT( "wxWizardPageSimple" ) ) {
      XRC_MAKE_INSTANCE( p, wxWizardPageSimple )
      p->Create( m_wizard, NULL, NULL, GetBitmap() );
      if( m_lastSimplePage ) {
        wxWizardPageSimple::Chain( m_lastSimplePage, p );
      }
      page = p;
      m_lastSimplePage = p;
    } else {
      if( !m_instance ) {
        wxLogError( wxT( "wxWizardPage is abstract class, must be subclassed" ) );
        return NULL;
      }
      page = wxStaticCast( m_instance, wxWizardPage );
      page->Create( m_wizard, GetBitmap() );
    }
    page->SetName( GetName() );
    page->SetId( GetID() );
    SetupWindow( page );
    CreateChildren( page );
    return page;
  }
}

bool wxWizardXmlHandler::CanHandle( wxXmlNode *node ) {
  return IsOfClass( node, wxT( "wxWizard" ) ) ||
         ( m_wizard != NULL &&
           ( IsOfClass( node, wxT( "wxWizardPage" ) ) ||
             IsOfClass( node, wxT( "wxWizardPageSimple" ) ) )
         );
}
